package com.lgj.auth.code.service;

import com.lgj.auth.content.OauthContents;
import com.lgj.auth.jdbc.UserRepository;
import com.lgj.core.LoginUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author: guangjie.liao
 * @Date: 2022/11/18 10:17
 * @Description:
 */
public class CustomerUserDetailService implements UserDetailsService {

    private UserRepository userRepository;

    public CustomerUserDetailService(UserRepository userServerService){
        this.userRepository = userServerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //处理用户登录
        LoginUser user = userRepository.getLoginUser(username);
        if (!user.isAccountNonExpired()){
            throw new UsernameNotFoundException(OauthContents.ACCOUNT_EXPIRED);
        }
        if (!user.isAccountNonLocked()){
            throw new UsernameNotFoundException(OauthContents.ACCOUNT_LOCKED);
        }
        if (!user.isAccountNonExpired()){
            throw new UsernameNotFoundException(OauthContents.ACCOUNT_DISABLED);
        }
        return user;
    }


}
